const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

// 定义职位申请模型
const Application = sequelize.define('Application', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  jobId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'jobs',
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  },
  resumeId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'resumes',
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'CASCADE'
  },
  status: {
    type: DataTypes.ENUM('pending', 'reviewing', 'interview', 'offer', 'rejected'),
    defaultValue: 'pending',
    allowNull: false
  },
  applicationDate: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    allowNull: false
  },
  feedback: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  interviewTime: {
    type: DataTypes.DATE,
    allowNull: true
  },
  interviewLocation: {
    type: DataTypes.STRING,
    allowNull: true
  }
}, {
  tableName: 'applications',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['job_id'] },
    { fields: ['resume_id'] },
    { fields: ['status'] },
    { fields: ['application_date'] }
  ]
});

// 关联关系
Application.associate = (models) => {
  // 申请属于职位
  Application.belongsTo(models.Job, { foreignKey: 'jobId', as: 'job' });
  // 申请属于简历
  Application.belongsTo(models.Resume, { foreignKey: 'resumeId', as: 'resume' });
};

module.exports = Application;